home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / pwrclib.exe / DIRSELF.C < prev    next >
C/C++ Source or Header  |  1992-10-01  |  17KB  |  482 lines

  1. #include "STDIO.h"
  2. #include "STDLIB.h"
  3. #include "IO.h"
  4. #include "FCNTL.h"
  5. #include "DOS.h"
  6. #include "DIR.h"
  7.  
  8. /*
  9.     DIRSEL.c   v1.00    Vernon E. Davis
  10.     07/12/87            17 South Centre Street
  11.                         Merchantville, NJ 08109
  12.                         CompuServe [71330,2705]
  13.                         GEnie - VED
  14.  
  15.                         Adapted for Power C 2.0.1 by
  16.                         Chuck Steenburgh [72330,1776]
  17.  
  18.  
  19.     purpose   : to select one filename from the list presented
  20.     syntax    : #include "STDIO.h"
  21.               : #include "STDLIB.h"
  22.               : #include "IO.h"
  23.               : #include "FCNTL.h"
  24.               : #include "DOS.h"
  25.               : #include "DIR.h"
  26.               : *p = dirselect(*mask,attr);
  27.               : char *p;     the filename
  28.               : char *mask;  the directory mask, i.e. "*.*"
  29.               : int attr;    the attribute to look for,i.e.
  30.               :               FA_RDONLY, FA_HIDDEN, FA_SYSTEM,
  31.               :               FA_LABEL,  FA_DIREC,  FA_ARCH.
  32.               :               (see dos.h or Turbo C manual for info.)
  33.               :
  34.     returns   : either a pointer to a filename that was selected or
  35.               : NULL if Escape was pressed and if no file is found.
  36.               :
  37.     descrip.  : a call to dirselect() first determines if there are any files
  38.               : that meet the specs received from the directory mask. If
  39.               : no files were found, the function immediately returns NULL.
  40.               : Otherwise, all filenames that meet the mask spec are copyed
  41.               : into an array that can hold up to 120 selections. The routine
  42.               : then clears a portion of the screen, saving the contents of
  43.               : the screen in an array. A frame is drawn in the cleared portion
  44.               : of the screen and the current directory contents are displayed
  45.               : in the boxed area as per the  mask that was received. The first
  46.               : selection is highlighted in reverse video. The up/down/left/
  47.               : right arrow keys will move the highlight around.  The Home Key
  48.               : returns to the first selection. The End key moves to the last
  49.               : selection. When < CR > is pressed,  the screen returns to
  50.               : normal, the cursor is placed in its original position and the
  51.               : value returned from the function is a pointer to a string
  52.               : containing the filename selected.  If Esc was pressed instead,
  53.               : the previous sequence is performed, with the exception of NULL
  54.               : being returned instead of the filename.
  55.               :
  56.     notes     : A. All routines in this file can be run under TC, the
  57.               :    Turbo C Integrated Development Environment. Because of
  58.               :    this, the routine will run slow when executed. It was
  59.               :    not designed for speed.
  60.               : B. These routines rely on the video hardware interrupt 0x10
  61.               :    for the cursor information. Insure that the PC is an IBM
  62.               :    or compatable.
  63.               : C. Three (3) cursor functions and a frame drawing function are
  64.               :    available. They are as follows:
  65.               :    1. void     gotoxy(x,y); move cursor to new location
  66.               :                int x (0..79)
  67.               :                int y (0..24)
  68.               :    2. void     cursor(ON/OFF); turn cursor on or off
  69.               :                int ON (1) or OFF (0)
  70.               :    3. xyposdef wherecur(void); locate cursor position
  71.               :                xyposdef.x (0..79)  column location
  72.               :                xyposdef.y (0..24)  row location
  73.               :    4. void     drframe(top,left,bottom,right); draw double fr.
  74.               :                int top    (0..24)  top corner
  75.               :                int left   (0..79)  left corner
  76.               :                int bottom (0..24)  bottom corner
  77.               :                int right  (0..79)  right corner
  78.               :    The other functions declared are special to dirselect()
  79.               :    and of no real use; however, they are documented.
  80. */
  81.  
  82. #define TRUE 1
  83. #define FALSE 0
  84. #define ON 1
  85. #define OFF 0
  86. #define VIDINTR 0x10
  87. #define EQUINTR 0x11
  88. #define FPOS1  1
  89. #define FPOS2 14
  90. #define FPOS3 27
  91. #define FPOS4 40
  92. #define FPOS5 53
  93. #define FPOS6 66
  94.  
  95.    typedef struct     /* structure for directory array */
  96.    {  char fname[13];
  97.    }  dirdef;
  98.  
  99.    typedef struct     /* structure for cursor column(x) and row(y) */
  100.    {  int x,y;
  101.    }  xyposdef;
  102.  
  103. /*  function declarations  */
  104.  
  105. char* dirselect(char*,int);                   /* the main function */
  106. void hilite(unsigned char,unsigned char);     /* highlights the filename */
  107. xyposdef fnamepos(unsigned char);             /* X/Y position of filename */
  108. void gotoxy(unsigned char,unsigned char);     /* move cursor to new location */
  109. void cursor(int);                             /* turn cursor on or off */
  110. xyposdef wherecur(void);                      /* current cursor location */
  111. int menubox(int r1, c1, r2, c2, color, boxtype, shadow, shade);  /* See file MENUBOX.C */
  112.  
  113. char* dirselect(mask,attr)
  114.    char mask[13];
  115.    int attr;
  116. {  union REGS regs;
  117.    struct ffblk ffblk;
  118.    int h,i,j,k,row,error,oldcurx,oldcury;
  119.    unsigned char oldpos,newpos;
  120.    char chrbuf[4000],atrbuf[4000];
  121.    char key;
  122.    dirdef dos_dir[121],*dos_dirptr;
  123.    xyposdef xypos;
  124.    h=0;
  125.    if((error=findfirst(mask,&ffblk,attr))==-1) /* if not found, return NULL */
  126.       return(NULL);
  127.    while(!error&&h!=120)        /* else, collect filenames */
  128.    {  dos_dirptr=&dos_dir[h++];
  129.       strcpy(dos_dirptr->fname,ffblk.ff_name);
  130.       error=findnext(&ffblk);
  131.    };
  132.    xypos=wherecur();     /* store old cursor position */
  133.    oldcurx=xypos.x;
  134.    oldcury=xypos.y;
  135.    cursor(OFF);
  136.    k=0;
  137.    for(i=0;i<((h/6)+3);i++)   /* store old screen info & clear portion */
  138.    {  for(j=0;j<80;j++)
  139.       {  gotoxy(j,i);
  140.          regs.h.bh=0;
  141.          regs.h.ah=8;
  142.          int86(VIDINTR,®s,®s);
  143.          chrbuf[k]=regs.h.al;
  144.          atrbuf[k]=regs.h.ah;
  145.          putch(32);
  146.          k++;
  147.       };
  148.    };
  149.    gotoxy(0,0);
  150.    i=0; j=0; row=1;
  151.    while(TRUE)                  /* display all filenames */
  152.    {  dos_dirptr=&dos_dir[i];
  153.       xypos=fnamepos(i);
  154.       gotoxy(xypos.x,xypos.y);
  155.       printf("%-012.12s",dos_dirptr->fname);
  156.       i++; j++;
  157.       if(i==h||i==120) break;
  158.       if(j>5)
  159.       {  j=0;
  160.          row++;
  161.          printf("\n");
  162.       };
  163.    };
  164.    menubox(0,0,(row+1),79,15,3,0,1);  /* draw the frame */
  165.    hilite(255,0);            /* highlight the first filename */
  166.    oldpos=0;
  167.    newpos=0;
  168.    while(TRUE)           /* get keypress and do appropriate action */
  169.    {  key=getch();
  170.       switch(key)
  171.       {  case 27:  /* Esc  */
  172.             k=0;
  173.             for(i=0;i<((h/6)+3);i++)  /* return contents of old screen */
  174.             {  for(j=0;j<80;j++)
  175.                {  gotoxy(j,i);
  176.                   regs.h.al=chrbuf[k];
  177.                   regs.h.bl=atrbuf[k];
  178.                   regs.h.ch=0;
  179.                   regs.h.cl=1;
  180.                   regs.h.bh=0;
  181.                   regs.h.ah=9;
  182.                   int86(VIDINTR,®s,®s);
  183.                   k++;
  184.                };
  185.             };
  186.             gotoxy(oldcurx,oldcury);         /* return old cursor position */
  187.             cursor(ON);
  188.             return(NULL);                    /* return NULL */
  189.          case 71:  /* Home */                /* goto first filename */
  190.             oldpos=newpos;
  191.             newpos=0;
  192.             hilite(oldpos,newpos);
  193.             break;
  194.          case 79:  /* End  */                /* goto last filename */
  195.             oldpos=newpos;
  196.             newpos=(h-1);
  197.             hilite(oldpos,newpos);
  198.             break;
  199.          case 72:  /* Up   */                /* move up one filename */
  200.             i=newpos;
  201.             i-=6;
  202.             if(i>=0)
  203.             {  oldpos=newpos;
  204.                newpos=i;
  205.